#!/bin/bash

DMSTRING=$(sudo /usr/sbin/dmsetup status |grep  -P -- '\d+\W+cache\W+\d+' | head -1)
# example:
#  11617255424 cache 8 3005/12288 1984 998185/998185 227992434 36220107 294608588 12215083 0 0 861691 3 metadata2 writethrough no_discard_passdown 2 migration_threshold 2048 cleaner 0 rw -

IFS=' ' read -a POS <<< "${DMSTRING}"
metadata_blocksize="${POS[4]}"
used_metadata_blocks="${POS[5]%%/*}"
total_metadata_blocks="${POS[5]##*/}"
cache_block_size="${POS[6]}"
used_cache_blocks="${POS[7]%%/*}"
total_cache_blocks="${POS[7]##*/}"
read_hits="${POS[8]}"
read_misses="${POS[9]}"
write_hits="${POS[10]}"
write_misses="${POS[11]}"
demotions="${POS[12]}"
promotions="${POS[13]}"
dirty="${POS[14]}"



echo "{
\"used_metadata_blocks\": $used_metadata_blocks,
\"total_metadata_blocks\": $total_metadata_blocks,
\"used_cache_blocks\": $used_cache_blocks,
\"total_cache_blocks\": $total_cache_blocks,
\"read_hits\": $read_hits,
\"read_misses\": $read_misses,
\"write_hits\": $write_hits,
\"write_misses\": $write_misses,
\"demotions\": $demotions,
\"promotions\": $promotions,
\"dirty\": $dirty
}"
